<template>
	<div class="login-wrap">
		<div class="ms-login">
			<div class="ms-title">中电长城停车场-后台管理系统</div>
			<el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
				<!-- 表单 -->
				<el-form-item prop="administrator_phone">
					<el-input v-model="param.administrator_phone" placeholder="请输入账号">
						<template #prepend>
							<el-button :icon="User"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<el-form-item prop="administrator_pwd">
					<el-input
						type="password"
						placeholder="请输入密码"
						v-model="param.administrator_pwd"
						@keyup.enter="submitForm(login)"
					>
						<template #prepend>
							<el-button :icon="Lock"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="submitForm(login)">登录</el-button>
				</div>
				<p class="login-tips">Tips : 用户名和密码随便填。</p>
			</el-form>
		</div>
	</div>
</template>

<script setup lang="ts">


// 导入函数
import { ref, reactive } from 'vue';
import { useTagsStore } from '../store/tags';
import { usePermissStore } from '../store/permiss';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { Lock, User } from '@element-plus/icons-vue';

// 导入网络请求API
import { loginFn } from '../api/index'

// 接口规范
interface LoginInfo {
	administrator_phone: string;
	administrator_pwd: string;
}
// 创建路由对象
const router = useRouter();

// 记录表单信息
const param = reactive<LoginInfo>({
	administrator_phone: 'admin',
	administrator_pwd: ''
});

// 表单规则(判断表单是否为空)
const rules: FormRules = {
	administrator_phone: [{required: true,message: '请输入账号',trigger: 'blur'}],
	administrator_pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};

const permiss = usePermissStore();
const login = ref<FormInstance>();

// 点击 “登录” 按钮
// 账号： admin 密码：admin
// 账号:  root  密码: root
const submitForm = (formEl: FormInstance | undefined) => {
	// console.log('1.0',formEl)
	// console.log('2.0',param)
	if (!formEl) return;
	formEl.validate( async (valid: boolean) => {
		if (valid) {
			// 获取表单信息
			let {administrator_phone, administrator_pwd} = param;
			// 执行管理员的登录接口
		    await loginFn({administrator_phone, administrator_pwd})
			.then(
				(content)=> {
					// console.log("x.0",content)
					let { code , msg, token , administrator_phone, administrator_name, administrator_role} = content;
					if(code == 200) {
						// 登录成功
						ElMessage.success(msg);
						// 把账号储存在客户端本地
						localStorage.setItem('administrator_phone', administrator_phone);
						localStorage.setItem('administrator_name', administrator_name);
						localStorage.setItem('administrator_role', administrator_role);
						// 把token储存在本地
						localStorage.setItem('auto_token', token);
						// 判断用户权限
						const keys = permiss.defaultList[administrator_role == 'administrator' ? 'admin' : 'user'];
						permiss.handleSet(keys);
						localStorage.setItem('ms_keys', JSON.stringify(keys));
						// 跳转页面
						router.push('/');

					}
					else {
						// 登录失败。
						ElMessage.error(msg);
					}
				},
				(error)=> {
					// console.log("y.0",error)
					ElMessage.error('登录失败。');
				}
			)

		} else {

			// 错误提示弹窗
			ElMessage.error('输入框不能为空');
			// console.log('yyyy');
			return false;
		}
	});
};

const tags = useTagsStore();
tags.clearTags();

</script>

<style scoped>
.login-wrap {
	position: relative;
	width: 100%;
	height: 100%;
	background-image: url(../assets/img/login-bg.jpg);
	background-size: 100%;
}
.ms-title {
	width: 100%;
	line-height: 50px;
	text-align: center;
	font-size: 20px;
	color: #fff;
	border-bottom: 1px solid #ddd;
}
.ms-login {
	position: absolute;
	left: 50%;
	top: 50%;
	width: 350px;
	margin: -190px 0 0 -175px;
	border-radius: 5px;
	background: rgba(255, 255, 255, 0.3);
	overflow: hidden;
}
.ms-content {
	padding: 30px 30px;
}
.login-btn {
	text-align: center;
}
.login-btn button {
	width: 100%;
	height: 36px;
	margin-bottom: 10px;
}
.login-tips {
	font-size: 12px;
	line-height: 30px;
	color: #fff;
}
</style>
